﻿
$(document).ready(function () {
    $("#tabs").tabs();
    $("#reloadPage").button(
    {
        icons: {
            primary: 'ui-icon-refresh'
        },
        text: false
    }
    ).click(function () {
        window.location.reload();
    })
});

var progressStatus = -1;
///Called when a newsletter row is selected
function onRowNewsletterSelected(e) 
{
    var id = e.row.cells[0].innerHTML;
    var urlNewsletter = e.row.cells[2].innerHTML;

    $("#dialog-confirmNewsletter").dialog({
        resizable: false,
        height: 300,
        width: 450,
        modal: true,
        title: '<a href="' + urlNewsletter + '" target="_blank">Newsletter ' + id + '</a>',
        buttons: {
        //cancel
            'Annuler': function () {
                $(this).dialog('close');
            },
            //Send the newsletter to the current user only
            'M\'envoyer la newsletter': function () {
                $.post(adresseEnvoiMail, { onlyCurrentUser: true, idNewsletter: id });
                progressStatus = -1;
                $(this).dialog('close');
                AfficherUpdateNewsletterSendStatusFenetre();
                //window.location = adresseEnvoiMail + "?onlyCurrentUser=true&idNewsletter=" + id;

            },
            //Pulicate the newsletter
            'Publier': function () {
                $.post(adresseEnvoiMail, { onlyCurrentUser: false, idNewsletter: id });
                progressStatus = -1;
                $(this).dialog('close');
                AfficherUpdateNewsletterSendStatusFenetre();
                //window.location = adresseEnvoiMail + "?onlyCurrentUser=false&idNewsletter=" + id;


            }

        }
    });
}

//Called every second to update newsletter status when it has been send.
//Updates a progress bar
function UpdateNewsletterSendStatus() {
    $.ajax({
        url: adresseUpdateNewsletterSendStatus,
        success: function (data) {

            if (data.max == -1) {
                if (progressStatus >= 0) {
                    progressStatus = 100;
                    $("#progressbar").progressbar({
                        value: 100
                    });
                    return;
                }
            }
            else if (data.max == 0) {
                progressStatus = 100;
            }
            else {
                progressStatus = Math.round((data.current / data.max) * 100);
            }
            if (progressStatus != -1) {
                $("#progressbar").progressbar({
                    value: progressStatus
                });
            }
            else {
                $("#progressbar").progressbar({
                    value: 0
                });
            }


            setTimeout("UpdateNewsletterSendStatus()", 1000);
        }
    });
}

function AfficherUpdateNewsletterSendStatusFenetre() {

    $("#progressbar").progressbar();
    //Contains the progress bar
    $("#dialog-modal").dialog({
        height: 200,
        width: 800,
        modal: true
    });

    UpdateNewsletterSendStatus();
}

function onRowSelected(e) 
{

    var username = e.row.cells[0].innerHTML;
    $("#dialog-confirm").dialog({
        resizable: false,
        height: 190,
        width: 400,
        modal: true,
        title: 'Supprimer ' + username,
        buttons: {
            'Oui': function () {
                window.location = adresseSupression + "?username=" + username;
                $(this).dialog('close');

            },
            'Non, ne pas le supprimer': function () {
                $(this).dialog('close');
            }
        }
    });
}


